﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PromotionApp.Model;
using PromotionApp.BLL;
using PromotionApp.Utility;

namespace PromotionApp.UserControl
{
    public partial class ucTopic : System.Web.UI.UserControl
    {
        #region Protected Variable

        protected string _ConfirmActionMessage = string.Empty;

        #endregion

        #region Property

        public bool IsBackend
        {
            get
            {
                bool isBackend = false;

                if (ViewState["IsBackend"] != null)
                    isBackend = Convert.ToBoolean(ViewState["IsBackend"]);

                return isBackend;
            }
            set
            {
                ViewState["IsBackend"] = value;
            }
        }

        public int TopicId
        {
            get
            {
                int topicId = 0;

                if (ViewState["TopicId"] != null)
                    topicId = Convert.ToInt32(ViewState["TopicId"]);

                return topicId;
            }
            set
            {
                ViewState["TopicId"] = value;
            }
        }

        public string StatusCode
        {
            get
            {
                string strStatusCode = string.Empty;

                if (ViewState["StatusCode"] != null)
                    strStatusCode = ViewState["StatusCode"].ToString().Trim();

                return strStatusCode;
            }
            set
            {
                ViewState["StatusCode"] = value;
            }
        }

        public InternalUser User
        {
            get
            {
                InternalUser user = new InternalUser();

                if (ViewState["InternalUser"] != null)
                    user = ViewState["InternalUser"] as InternalUser;

                return user;
            }
            set
            {
                ViewState["InternalUser"] = value;
            }
        }

        #endregion

        #region Private Method

        private void InitializeImage(string strImagePath)
        {
            if (strImagePath.Length > 0)
                this.imgReply.ImageUrl = strImagePath;
            else
                this.trImage.Visible = false;
        }

        private void IntializePostedBy(string strUseName, string strEmail)
        {
            if (strUseName.Length > 0)
                this.lblPostedBy.Text = strUseName;
            else if (strEmail.Length > 0)
                this.lblPostedBy.Text = strEmail;
        }

        private void InitializeStatus(string strStatusCode)
        {
            if (this.IsBackend)
            {
                if (strStatusCode.Equals("E"))
                {
                    this.lblStatusDesc.Text = "Enable";
                    this.btnAction.CssClass = "btnTurnOff";
                    this.btnAction.ToolTip = "[Disable this topic]";
                    this.btnAction.OnClientClick = "return confirm('Are you sure to want to turn off this topic ?')";
                }
                else if (strStatusCode.Equals("P"))
                {
                    this.lblStatusDesc.Text = "Disable";
                    this.btnAction.CssClass = "btnTurnOn";
                    this.btnAction.ToolTip = "[Enable this topic]";
                    this.btnAction.OnClientClick = "return confirm('Are you sure to want to turn on this topic ?')";
                }
                else if (strStatusCode.Equals("D"))
                {
                    this.lblStatusDesc.Text = "Delete";
                    this.btnAction.Visible = false;
                    this.btnDelete.Visible = false;
                }
            }
        }

        private void Initialize()
        {
            Topic topic = TopicBLL.GetInstance().GetTopicById(this.TopicId);

            this.lblTopic.Text = topic.TopicDescription;
            this.lblMessage.Text = topic.Message;
            InitializeImage(topic.ImagePath);
            IntializePostedBy(topic.UserName, topic.Email);
            this.lblPostedDate.Text = topic.PostedDate.ToString(ConfigurationHelper.DateTimeDisplayFormat);
            this.trStatus.Visible = this.IsBackend ? true : false;
            InitializeStatus(topic.StatusCode);
            this.StatusCode = topic.StatusCode;
        }

        private void ChangeStatus()
        {
            if (this.StatusCode.Equals("E"))
                TopicBLL.GetInstance().PendingTopic(this.TopicId, 1); //Mock up user id
            else if (this.StatusCode.Equals("P"))
                TopicBLL.GetInstance().EnableTopic(this.TopicId, 1); //Mock up user id            

            Initialize();
        }

        private void Delete()
        {
            TopicBLL.GetInstance().DeleteTopic(this.TopicId, 1); //Mock up user id
            Initialize();
        }

        #endregion

        #region Event

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                try
                {
                    Initialize();
                }
                catch (Exception ex)
                {
                    PageHelper.AlertMessage(ex.Message, this.Page);
                }
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                Delete();
            }
            catch (Exception ex)
            {
                PageHelper.AlertMessage(ex.Message, this.Page);
            }
        }

        protected void btnAction_Click(object sender, EventArgs e)
        {
            try
            {
                ChangeStatus();
            }
            catch (Exception ex)
            {
                PageHelper.AlertMessage(ex.Message, this.Page);
            }
        }

        #endregion
    }
}